package com.victor9ac.mashup.usecase.auxiliary;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.w3c.dom.Text;

import com.victor9ac.mashup.api.Comment;
import com.victor9ac.mashup.api.Commentaries;

public class CleanupTwitterComment extends CleanupComment {



//	protected static final String TWEET_HASHTAG_PATTERN = "#([0-9]*[A-Z]*[a-z]*[0-9][a-z]*[A-Z]* )*";
//	protected static final String TWEET_USER_PATTERN = "@([0-9]*[A-Z]*[a-z]*[0-9][a-z]*[A-Z]* )*";
	protected static final String TWEET_USER_PATTERN = "@([A-Za-z0-9_]+)";
	protected static final String TWEET_HASHTAG_PATTERN = "#([A-Za-z0-9_]+)";


	@Override
	public Commentaries cleanupCommentaries(Commentaries commentaries) {

		for (Comment comment : commentaries) {

			cleanRetweets(comment);
			cleanUrl(comment);
			cleanTweetMetadata(comment);
			ajustWhiteSpaces(comment);
			
		}
		return commentaries;
	}
	
	private void ajustWhiteSpaces (Comment c){
		String str = getText(c);
		c.setText(str.replaceAll("\\s+", " "));
		
	}

	@Override
	public void cleanRetweets(Comment comment) {
		
		String text = getText(comment);
		
		String pattern = "RT ";
		
		StringBuffer sb = replace(new StringBuffer(text), pattern, "");
		
		comment.setText(sb.toString());
		
		
	}

	@Deprecated
	@Override
	public void cleanUrl(Comment comment) {
		
		
		
		try {
			String url = extractUrlFromComments(comment.getTextRaw());
			if (url != null) {
				comment.getUrlCommentImages().add(url);
				removeUrlFromText(comment, url);
			}
		} catch (Exception e) {
			System.out
					.println("exception extracting url from comment, will be ignored this case: "
							+ e.getMessage());
			e.printStackTrace();
		}

	}

	void removeUrlFromText(Comment comment, String url) {
		String text = getText(comment);
		if (text != null) {
			String textWithoutUrl = text.substring(0, text.indexOf(url));
			comment.setText(textWithoutUrl);
		}

	}

	private String getText(Comment comment) {
		String text = comment.getText();
		if (text == null) {
			text = comment.getTextRaw();
		}
		return text;
	}

	@Deprecated
	String extractUrlFromComments(String text) {
		String url = null;
		if (text.contains("http:")) {

			int urlIndex = text.indexOf("http:");

			url = text.substring(urlIndex, text.length());

			if (url.contains(" ")) {
				url = url.substring(0, url.indexOf(" "));
			}
		}
		return url;
	}


	@Deprecated
	String cleanRetweets(String text) {
		String result = "";

		int lastRT = text.lastIndexOf("RT");

		if (lastRT >= 0) {
			result = text.substring(lastRT, text.length());
			int startComment = result.indexOf(":") + 1;

			result = result.substring(startComment, result.length());

		} else {
			result = text;
		}

		return result;
	}

	@Override
	protected void cleanTweetMetadata(Comment comment) {
		String text = getText(comment);
		
		String userTwitterPattern = TWEET_USER_PATTERN;		
		StringBuffer sb  = replace(new StringBuffer(text), userTwitterPattern, "");

		String hashtagTwitterPattern = TWEET_HASHTAG_PATTERN;	
		sb  = replace(sb, hashtagTwitterPattern, "");
		
		comment.setText(sb.toString().trim());
		
	}

	private StringBuffer replace(StringBuffer text, String pattern,
			String replacement) {
		StringBuffer auxStringBuffer = new StringBuffer();
		try {
			Matcher matcher = Pattern.compile(pattern).matcher(text);
			while (matcher.find()) {
				matcher.appendReplacement(auxStringBuffer, replacement);
			}
			matcher.appendTail(auxStringBuffer);
		} catch (Exception e) {
			System.out.println("cannot parse it: "+text);
			auxStringBuffer = new StringBuffer(text);
		}
		
		

		return auxStringBuffer;
	}

}
